﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Public_Class_Library
{
    /// <summary>
    /// 跨服务访问其他服务接口
    /// </summary>
    public class Cross_service_Helper
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public Cross_service_Helper(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }
        /// <summary>
        /// 跨服务访问接口-必须是HttpGet协议，否则报405
        /// </summary>
        /// <returns></returns>
        public async Task<string> Get(string requestUri)
        {
            //改一下IP号10和System_number_tableRead是要用的方法名
            // http://10.31.59.10:5185/api/System_number_tableRead  六子开接口地址模版
            //通过httpclient调用 别的Api服务的方法 返回
            var httpClient = _httpClientFactory.CreateClient();
            var response = httpClient.GetAsync(requestUri).Result;
            //HttpClient client=new HttpClient();
            //通过httpclient 来访问 httpURL
            //IsSuccessStatusCode判断是否成功
            if (response.IsSuccessStatusCode)
            {
                //成功返回对象
                var message = response.Content.ReadAsStringAsync().Result;
                //var obj = JsonConvert.DeserializeObject(message);
                return message;
            }
            //失败返回响应结果
            return string.Empty;
        }
    }


}
